import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login'
import home from '@/components/home'
import my from '@/components/my/my'
import mycarinfo from '@/components/my/mycarinfo'
import myhazard from '@/components/my/myhazard'
import myhome from '@/components/my/myhome'
import mysetting from '@/components/my/mysetting'
import mymodify from '@/components/my/mymodify'
import list from '@/components/list/list'
import order from '@/components/basecom/order'
import hisorder from '@/components/basecom/hisorder'
import preorder from '@/components/basecom/preorder'
import map from '@/components/map/map'
import error from '@/components/error'
import axios from '@/api/axios'
import store from '@/store/index'
import {Toast} from 'mint-ui'
Vue.use(Router)

export default new Router({
  routes: [
  	{
  		name: 'index',
  		path: '/',
  		redirect: '/home'
  	},
  	{
  		name: 'login',
  		path: '/login',
  		component: login
  	},
    {
    	name: 'home',
    	path: '/home',
    	meta: {
    		requiresAuth: true
    	},
        beforeEnter: (to, from, next) => {
          if (to.matched.some(record => record.meta.requiresAuth)) {
        		var token = store.getters.getToken;
        		if(token) {
	  				next();
	  			}else {
	  				next({
		                path: '/login'
		            });
	  			}
  			}else {
  				next();
  			}
  		},
    	component: home,
    	children: [
    		{
    			name: 'h-default',
    			path: '/home/',
    			component: map
    		},
    		{
    			name: 'map',
    			path: '/home/map',
    			component: map
    		},
    		{
    			name: 'list',
    			path: '/home/list',
    			component: list,
          beforeEnter: (to, from, next) => {
            var setting = {
                  method: 'POST',
                  url: '/getsetTruckSingleState'
                };
            var data = {
                  phone: store.getters.getId,
                  ckgrp: 'get_state'
                }
                axios((res) => {
                  var state = null;
                  if (res.code === 0) {
                    state = res.data.singlestate === 1 ? true : false; 
									}else{
                    state = res.data.singlestate === 1 ? true : false;
                    Toast('网络错误');
                  }
                  store.commit('setStartOrder', state);
                  next();
                }, {setting: setting, data: data});
          },
    			children: [
    				{
    					name: 'order',
    					path: '/home/list/order/:orderid',
    					component: order,
    					beforeEnter:  (to, from, next) => {
    						var ordered = to.query.ordered;
    						if (ordered && ordered !== 'false') {
    							next();
    						}else {
                  Toast('当前无订单');
    							next({path: from.fullPath});
    						}
    					}
    				},
    				{
    					name: 'hisorder',
    					path: '/home/list/hisorder/:orderid',
    					component: hisorder
    				},
    				{
    					name: 'preorder',
    					path: '/home/list/preorder/:orderid',
    					component: preorder,
              beforeEnter: (to, from, next) => {
                var query = to.query;
                var id = query.id;
                var userid = store.getters.getId;
                var setting = {
                  method: 'GET',
                  url: '/getTruckOrderDetailed/' + userid + '/' + id
                };
                axios((res) => {
                  if(res.code === 0) {
                    var msg = res.data;
                    store.commit('setAcStoreMsg', msg);
                    next();
                  }else {
                    if(res.code === 401) {
                      Toast(res.data);
                      next({path: from.fullPath});
                    }
                  }
                }, {setting: setting});
              }
    				}
    			]
    		},
    		{
    			name: 'my',
    			path: '/home/my',
				component: my,
				children: [
					{
						name: myhome,
						path: '/home/my/myhome',
						component: myhome,
					},
					{
						name: 'mycarinfo',
						path: '/home/my/mycarinfo',
						component: mycarinfo
					},
					{
						name: 'myhazard',
						path: '/home/my/myhazard',
						component: myhazard
					},
					{
						name: 'mysetting',
						path: '/home/my/mysetting',
						component: mysetting
					},
					{
						name: 'mymodify',
						path: '/home/my/mymodify',
						component: mymodify
					}
				]
    		}
    	]
    },
    {
    	name: 'error',
    	path: '*',
    	component: error
    }
  ]
})
